package tech.yunjing.train.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import tech.yunjing.train.entity.Account;
import tech.yunjing.train.service.AccountService;
import tech.yunjing.train.util.JsonResult;

/**
 * 账户管理的controller
 * Created by ZhangFan on 2017/6/8.
 */
@RestController
@RequestMapping("/account")
public class AccountController extends BaseController {
    @Autowired
    AccountService accountService;

    /**
     * 根据账户名获取账户信息(不含支付密码信息)
     *
     * @param accountName 账户名
     * @return 账户对象
     */
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public JsonResult<Account> findAccountInfo(String accountName) {
        Account account = accountService.findByAccountName(accountName);
        return new JsonResult<Account>(account);
    }

    /**
     * 新增账户
     *
     * @param account 账户对象
     * @return 增加记录条数
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JsonResult<Integer> addAccount(Account account) {
        int row = accountService.insertAccount(account);
        return new JsonResult<Integer>(row);
    }

    /**
     * 根据账户名改变用户状态实现逻辑删除
     *
     * @param accountName 账户名
     * @return 删除记录条数
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public JsonResult<Integer> delAccount(String accountName) {
        int row = accountService.deleteAccount(accountName);
        return new JsonResult<Integer>(row);
    }

    /**
     * @param accountName        账户名
     * @param oldPaymentPassword 旧支付密码
     * @param newPaymentPassword 新支付密码
     * @return 更改记录数
     */
    @RequestMapping(value = "/modifyPassword", method = RequestMethod.POST)
    public JsonResult<Integer> modifyPassword(String accountName, String oldPaymentPassword, String newPaymentPassword) {
        int row = accountService.updatePaymentPasswordByAccountName(accountName, oldPaymentPassword, newPaymentPassword);
        return new JsonResult<Integer>(row);
    }

    /**
     * 根据账户名更改余额
     *
     * @param accountBalance 账户余额
     * @param accountName    账户名
     * @return 更改记录条数
     */
    @RequestMapping(value = "/updatebalance", method = RequestMethod.POST)
    public JsonResult<Integer> update(Double accountBalance, String accountName) {
        int row = accountService.updateBalanceByAccountName(accountBalance, accountName);
        return new JsonResult<Integer>(row);
    }
}
